<?php
	class User_User{
		/*
		 *返回所有管理员信息 
		 */
		public function getAdminList(){
			$sql = "select * from `zjx_admin_table` order by `z_userGrade`";
			$ret = mysql_query($sql);
			$arr = array();
			while($row=mysql_fetch_array($ret)){
				$arr[]=$row;
			}
			return $arr;
		}
		/*
		 *返回所指定管理员的所有信息 
		 */
		public function getAdminListById($id){
			$sql = "select * from `zjx_admin_table` where `z_id`=".$id;
			$ret = mysql_query($sql);
			return mysql_fetch_array($ret);
		}
		/*
		 *返回所指定管理员的所有信息 
		 */
		public function getAdminListByName($username){
			$sql = "select * from `zjx_admin_table` where `z_username`=".$username;
			$ret = mysql_query($sql);
			return mysql_fetch_array($ret);
		}
		/*
		 * 通过用户名返回指定管理员的指定信息
		 */
		public function getAdminUserInfo($username,$return='z_id'){
			$sql = "select `$return` as grade from `zjx_admin_table` where `z_username`='".$username.'\' limit 1';
			$ret = mysql_query($sql);
			$row = mysql_fetch_array($ret);
			return $row['grade'];
		}
		/*
		 * 通过用户ID返回指定管理员的指定信息
		 */
		public function getAdminUserInfoById($id,$return){
			$sql = "select `$return` as grade from `zjx_admin_table` where `z_id`='".$id.'\' limit 1';
			$ret = mysql_query($sql);
			$row = mysql_fetch_array($ret);
			
			return $row[0][$return];
		}
		/*
		 * 根据用户id删除用户信息
		 */
		public function delAdminUser($id){
			$sql = "DELETE FROM `zjx_admin_table` WHERE `zjx_admin_table`.`z_id` = $id";
			return mysql_query($sql);
		}
		/*
		 * 更新管理员登录记录
		 */
		public function updateAdminLoginState($username){
			$sql="update `zjx_admin_table` set `z_ip`='".$_SERVER['REMOTE_ADDR']."',`z_lasttime`='".date("Y-m-d H:i:s",time())."' where `z_username`='".$username."'";
			return mysql_query($sql);
		}
		/*
		 * 更新管理员密码
		 */
		public function updateUserPwdByName($pwd,$username){
			$sql="update `zjx_admin_table` set `z_userpwd`='".md5($pwd)."' where `z_username`='".$username."'";
			return mysql_query($sql);
		}
		/*
		 * 更新管理员密码
		 */
		public function updateUserPwdById($pwd,$id){
			$sql="update `zjx_admin_table` set `z_userpwd`='".md5($pwd)."' where `z_id`='".$id."'";
			return mysql_query($sql);
		}
		/*
		 * 更新管理员登录记录
		 */
		public function updateAdminInfo($arr){
			$sql="update `zjx_admin_table` set `z_username`='".$arr['username']."',`z_realname`='".$arr['realname']."',`z_userGrade`=".$arr['usergrade'].",`z_phone`='".$arr['userphone']."' where `z_id`='".$arr['userid']."'";
			return mysql_query($sql);
		}
		/*
		 * 添加管理员用户信息
		 */
		public function insertAdminUser($arr){
			$sql = "insert into `zjx_admin_table` values('','".$arr['username']."','".md5($arr['userpwd'])."','".$arr['realname']."','".$arr['usergrade']."','".$arr['userphone']."','".getIP()."',now())";
			return mysql_query($sql);
		}
	/**
	 * 有用户名称得到用户真实姓名
	 * 返回：存在真实姓名返回，无则返回用户名
	 */
		function getUserTrueNameByUsername($username){
			$realname = User_User::getAdminUserInfo($username,'z_realname');
			if($realname){
				return $realname;
			}
			return $username;
		}
	}